<?php
namespace TuxCocktail\TuxDb\Mysql\Cocktail;

use TuxCocktail\TuxDb\TuxCocktailSourceInterface;
use TuxCocktail\TuxDb\Mysql\Cocktail\Model\CocktailSourceTable;
use TuxCocktail\TuxDb\Mysql\Cocktail\Model\CocktailSourceRelationTable;
use TuxCocktail\TuxDb\Mysql\Cocktail\Model\CocktailSourceTypeTable;
use TuxCocktail\TuxDb\Mysql\TuxDbMysqlAbstract;


class TuxDbMysqlSourceType extends TuxDbMysqlAbstract implements TuxCocktailSourceInterface {
	
	/**
	 * 
	 * @var CocktailSourceTable
	 */
	private $cocktailSourceTable;
	
	/**
	 * 
	 * @var CocktailSourceRelationTable
	 */
	private $cocktailSourceRelationTable;
	
	/**
	 * 
	 * @var CocktailSourceTypeTable
	 */
	private $cocktailSourceTypeTable;
	
	private function getCocktailSourceTable() {
		if (!$this->cocktailSourceTable) {
			$this->cocktailSourceTable = new CocktailSourceTable($this->tuxDb->getInstance());
		}
		return $this->cocktailSourceTable;
	}
	
	private function getCocktailSourceRelationTable() {
		if (!$this->cocktailSourceRelationTable) {
			$this->cocktailSourceRelationTable = new CocktailSourceRelationTable($this->tuxDb->getInstance());
		}
		return $this->cocktailSourceRelationTable;
	}
	
	private function getCocktailSourceTypeTable() {
		if (!$this->cocktailSourceTypeTable) {
			$this->cocktailSourceTypeTable = new CocktailSourceTypeTable($this->tuxDb->getInstance());
		}
		return $this->cocktailSourceTypeTable;
	}
	
	public function getAllCocktailSourceTypes() {
		
		$select = $this->getCocktailSourceTypeTable()->newSelect();
		$this->getCocktailSourceTypeTable()->getSelect($select,array('cocktail_source_type_name'));
		$resultSet = $this->getCocktailSourceTypeTable()->selectWith($select);
		if ($resultSet->count() >0) {
			$arr = array();
			foreach ($resultSet as $row) {
				array_push($arr, $row->cocktail_source_type_name);
			}
			return $arr;
		}
		return null;		
		}
	
		public function addSource($cocktailSourceType,$cocktailSourceName,$cocktailSourceDesc) {
			$select = $this->getCocktailSourceTypeTable()->newSelect();
			$this->getCocktailSourceTypeTable()->getSelect($select,array('cocktail_source_type_id'))->where(array('cocktail_source_type_name'=>$cocktailSourceType));
			$resultSet = $this->getCocktailSourceTypeTable()->selectWith($select);
			if ($resultSet->count() != 1) {
				return null;
			}
			$arr = $resultSet->toArray();
			$cocktailSourceTypeId=$arr[0]['cocktail_source_type_id'];
			$cocktailSourceId = $this->getCocktailSourceTable()->createIfNotExist(array('cocktail_source_type_id'=>$cocktailSourceTypeId,'cocktail_source_name'=>$cocktailSourceName),array('cocktail_source_desc'=>$cocktailSourceDesc));
			return $cocktailSourceId;
		}
		
		public function updateSourceOfCocktail($cocktailId,$cocktailSourceType,$cocktailSourceName,$cocktailSourceDesc) {
			$cocktailSourceId = $this->addSource($cocktailSourceType, $cocktailSourceName, $cocktailSourceDesc);
			
			$sql = "insert into cocktail_source_relation(cocktail_id,cocktail_source_id) values(" . $cocktailTable->fp('cocktail_id') + "," + $cocktailTable->fp('cocktail_source_id') . ") on duplicate key update cocktail_source_id=" . $cocktailTable->fp('cocktail_source_id');
			$statement = $cocktailTable->getAdapter()->query($sql);
			$parameters = array(
					'cocktail_id'=>$$cocktailId,
					'cocktail_source_Id'=>$cocktailSourceId
			);
			$resultSet=$statement->execute($parameters);
				
			
			$this->getCocktailSourceRelationTable()->addCocktailSourceRelation($cocktailId, $cocktailSourceId);
		}
		
}